package me.zhengjie.modules.system.service.impl

import me.zhengjie.common.utils.PageUtil
import me.zhengjie.common.utils.QueryHelp
import me.zhengjie.common.utils.ValidationUtil
import me.zhengjie.modules.system.domain.DictDetail
import me.zhengjie.modules.system.repository.DictDetailRepository
import me.zhengjie.modules.system.service.DictDetailService
import me.zhengjie.modules.system.service.dto.DictDetailDTO
import me.zhengjie.modules.system.service.dto.DictDetailQueryCriteria
import me.zhengjie.modules.system.service.mapper.DictDetailMapper
import jakarta.persistence.criteria.CriteriaBuilder
import jakarta.persistence.criteria.CriteriaQuery
import jakarta.persistence.criteria.Root
import org.springframework.cache.annotation.CacheConfig
import org.springframework.cache.annotation.CacheEvict
import org.springframework.cache.annotation.Cacheable
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional

/**
 * @author Kuki Wu
 * @date 2019-04-10
 */
@Service
@CacheConfig(cacheNames = ["dictDetail"])
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = [Exception::class])
class DictDetailServiceImpl(
    private val dictDetailRepository: DictDetailRepository,
    private val dictDetailMapper: DictDetailMapper
) : DictDetailService {
    @Cacheable(key = "'queryall_dictname:'+#criteria.dictName")
    override fun queryAll(criteria: DictDetailQueryCriteria, pageable: Pageable?): Any? {
        val page =
            dictDetailRepository.findAll({ root: Root<DictDetail?>, criteriaQuery: CriteriaQuery<*>?, criteriaBuilder: CriteriaBuilder ->
                QueryHelp.getPredicate(
                    root,
                    criteria,
                    criteriaBuilder
                )
            }, pageable!!)
        return PageUtil.toPage(page.map { entity: DictDetail? -> dictDetailMapper.toDto(entity) })
    }

    @Cacheable(key = "'findbyid'+#p0")
    override fun findById(id: Long): DictDetailDTO? {
        val dictDetail = dictDetailRepository.findById(id).orElseGet { DictDetail() }
        ValidationUtil.isNull(dictDetail!!.id, "DictDetail", "id", id)
        return dictDetailMapper.toDto(dictDetail)
    }

    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = [Exception::class])
    override fun create(resources: DictDetail): DictDetailDTO? {
        return dictDetailMapper.toDto(dictDetailRepository.save(resources))
    }

    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = [Exception::class])
    override fun update(resources: DictDetail) {
        val dictDetail = dictDetailRepository.findById(resources.id!!).orElseGet { DictDetail() }
        ValidationUtil.isNull(dictDetail!!.id, "DictDetail", "id", resources.id!!)
        resources.id = dictDetail.id
        dictDetailRepository.save(resources)
    }

    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = [Exception::class])
    override fun delete(id: Long) {
        dictDetailRepository.deleteById(id)
    }
}